CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2004
    Posts
    131

    Not Crappy Image Loader?

    Is anyone aware of an easy way to load images into java or a good image loader library that is not horribly crappy? I'm getting tired of the inefficiency and horrible waste Java's built in image loader makes. (a 4mb image taking 1gb of ram?)

    Right now I just load them into a BufferedImage (using ImageIO), reduce their size, and then draw them to the screen.

  2. #2
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: Not Crappy Image Loader?

    You must be doing something wrong if loading a 4mb image takes a 1gb internal footprint. Show code. ImageIO works fine for me.
    "The Chicken and Rice MRE is not a personal lubricant."

  3. #3
    Join Date
    Apr 2004
    Posts
    131

    Re: Not Crappy Image Loader?

    Well regardless I'd like a better image loader anyway.

    I found this one:
    http://www.vlc.com.au/imageloader/
    but I can't get into the CVS repository to get the source, and I have neither a PC or Linux.

    I'll post some code from my program if you really want me to. And I'm not entirely sure the image itself takes up that much RAM, but here's my situation:

    I have a program I made that lets you classify large collections of images (like ones you may get from a digital camera) and saves all this information into an excel document, also resizing new copies of the images with different names.

    It loads images like this:
    Using ImageIO, it is stored in a temporary variable, resized to fit the window, and then saved into the BufferedImage that is eventually drawn to the screen. It also uses another thread to load up the next image in the series in the same way so that there is a buffer and no wait time between images. At most you will have two resized BufferedImages and one full sized BufferedImage in memory at a time. When you press the save and quit button, it systematically goes through all the information you've entered and saves it to an excel document, then saves copies of every image in jpg format better compressed a resized.

    The most memory you will ever use at once:
    10 JTextFields, some JPanels for formatting, 4 JLists to hold recent and common keywords, 2 resized BufferedImages, 1 full sized BufferedImage, and an ArrayList of String[]'s that contains the information you've entered that session.

    So, no, a single 4 mb image is not taking up the entire 1.4gb of ram my processor uses, but it is still an exhorbitant amount of memory.

  4. #4
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: Not Crappy Image Loader?

    It sounds like you are just complaining. I have done a comparison between ImageIO and perl using the ImageMagick library. They are the same speed to load, resize and save in another format. I could see using a different package if ImageIO didn't have the built in filters you needed for some effect but it doesn't sound like you are doing anything other than creating thumbnails.

    Reuse buffer space instead of allocating new ones.
    "The Chicken and Rice MRE is not a personal lubricant."

  5. #5
    Join Date
    Apr 2004
    Posts
    131

    Re: Not Crappy Image Loader?

    Okay, then how do I do that? I thought it was probably my inabilities but I have seen complaints of ImageIO from other people as well.

  6. #6
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: Not Crappy Image Loader?

    If you package this as an executable jar it will convert all bmp files in the directory it is run from to jpeg 800x600 with bilinear filtering.

    Code:
    package bmp2jpeg;
    
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    
    public class Main {
        public static void main(String[] args) {
            long starttime = System.currentTimeMillis();
            int count = 0;
            String directoryname = System.getProperty("user.dir");
            File log = new File(directoryname + System.getProperty("file.separator")+"bmp2jpeg.log");
            File dir = new File(directoryname);
            FilenameFilter filter = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith(".bmp");
                }
            };
            PrintWriter out = null;
            try{
                out = new PrintWriter(new FileWriter(log));
                File files[] = dir.listFiles(filter);
                for(count=0;count<files.length;count++){
                    String fileIn = files[count].getName();
                    String fileOut = fileIn.replaceFirst("\\.bmp","\\.jpeg");
                    //read in
                    BufferedImage bin = ImageIO.read(files[count]);
                    BufferedImage bout = new BufferedImage(800, 600,bin.getType());
                    // scale.
                    AffineTransform tx = new AffineTransform();
                    tx.scale(800.0/bin.getWidth(), 600.0/bin.getHeight());
                    AffineTransformOp top = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
                    top.filter(bin,bout);
                    //write out
                    ImageIO.write(bout,"jpeg",new File(fileOut));
                    out.println(fileIn + " converted to "+fileOut);
                }
                out.println(count+" bitmaps converted in "
                        +(System.currentTimeMillis()-starttime)/1000.0f
                        + " seconds.");
                out.close();
            } catch(IOException ex){
                ex.printStackTrace();
            }
        }
    }
    "The Chicken and Rice MRE is not a personal lubricant."

  7. #7
    Join Date
    Apr 2004
    Posts
    131

    Re: Not Crappy Image Loader?

    I don't really understand the difference here, other than running a completely separate program to do this particular piece and save a little bit of memory at a time. I'll show you my code later today, it's a lot like this (when an image is loaded it is automatically resized).

  8. #8
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: Not Crappy Image Loader?

    So it sounds like maybe you are holding on to references of the input bufferedimage and the garbage collector is not releasing them. Regardless of file size on the disk (jpeg is compressed), the internal data required to hold an image is width * height * colorbit depth. With a 1600x1200 camera image and assume 24bit colordepth, that is roughly 5.5MB per image. If you are holding images then you only need to process under 200 images of this size to be well over 1Gb of use.

    You are storing them in a Map?
    "The Chicken and Rice MRE is not a personal lubricant."

  9. #9
    Join Date
    Apr 2004
    Posts
    131

    Re: Not Crappy Image Loader?

    Good to know about how much memory it takes up per image.

    No, I'm not using a map, and I don't hold onto the images at all. When the user presses the next button, it loads the next image in the series into the buffer (a single BufferedImage), and the image they were just looking at is replaced by the old buffer (also a single BufferedImage). The information that was entered for that particular image is stored into an ArrayList containing string arrays of size eleven. The first ten array places are devoted to the text the user entered into the fields, while the last array position holds the filename of that image.

    When the user presses the save and quit button, the program cycles through the data saved in the strings and uses it to save this data to an excel document and then save copies with appropriate names as jpg's of each filename in the string array.

    Is that clear? Hopefully you can see what I'm doing. This maximizes speed until the very end, when the user can go get some coffee or something while they wait. I don't actually save the images anywhere beyond the currently displayed one and the next image in the series.

    Thanks again. Want code?

  10. #10
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: Not Crappy Image Loader?

    You can pm it to me if you don't want it public. I'm not really sure what this application is supposed to do. It is for creating metadata about images and storing it in excel format?
    "The Chicken and Rice MRE is not a personal lubricant."

  11. #11
    Join Date
    Apr 2004
    Posts
    131

    Re: Not Crappy Image Loader?

    No problem about it being public – it's not really for profit and I don't need to show you all the code anyway. I just haven't been at work so I haven't been able to send you the code. (I'm not there now either)

    Tomorrow I'll post it, hopefully I'll have time.

    Basically this is what it is for:
    My university's art department has acquired thousands of unnamed images that appear in vast series and all need to be put on a web server. In order to do this, meta data needs to be saved in an excel document and each image either needs to be renamed to an actual useful description or there must be a copy with a useful description so we can identifty them easily later.

    You are provided with fields to enter all the information for every image, it being displayed on the left at about 500x500 size, and it also keeps track of recently used keywords that you can click on to fill the current field.

    All said and done, you will have copies of all the images that are better compressed JPGs and an excel document with all the meta data saved. You can then use this to put nicely formatted images on the server.

    The only reason I made the program was because I had categorized 60 images manually and was sure I could save massive amounts of time making a program. It's not commercial.

    So that's what it does. I'll try to get some code up here later. If there's any copyright on it, I own it, so there's no problem getting it up here other than finding the time.

    Demonpants

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured