001    /*
002     * ImageLoadTester
003     * 
004     * Copyright (c) 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.apps;
009    
010    import java.io.File;
011    import net.sourceforge.jiu.codecs.ImageLoader;
012    import net.sourceforge.jiu.data.PixelImage;
013    import net.sourceforge.jiu.gui.awt.ToolkitLoader;
014    import net.sourceforge.jiu.ops.BatchProcessorOperation;
015    
016    /**
017     * Command line program that tries to load images from files,
018     * thus testing the built-in and / or Toolkit's codecs.
019     * Start the program with file names or directory names as arguments.
020     * Directory names will lead to the inclusion of that complete directory tree
021     * in the list of files to test.
022     * Add the argument <code>--notoolkit</code> in order to keep this 
023     * program from trying to load images via java.awt.Toolkit.
024     * The program will process all files and print one line per file
025     * that tells the result of the loading process.
026     * At the end, three lines with statistics (failed / successful / total
027     * loading attempts) are printed.
028     * Note that you may want to give the JVM more memory if large images
029     * are stored in those files you want to test.
030     * Example: 
031     * <pre>java -mx300m net.sourceforge.jiu.apps.ImageLoadTester *.jpg</pre>
032     * This gives 300 MB to the JVM.
033     * @author Marco Schmidt
034     * @since 0.11.0
035     */
036    public class ImageLoadTester extends BatchProcessorOperation
037    {
038            private boolean useToolkit;
039            private int numFailed;
040            private int numSuccess;
041    
042            /**
043             * Main method of this command line program.
044             * @param args program arguments, provided by the Java Virtual Machine, must be file or directory names
045             */
046            public static void main(String[] args) throws Exception
047            {
048                    ImageLoadTester tester = new ImageLoadTester();
049                    boolean useToolkit = true;
050                    for (int i = 0; i < args.length; i++)
051                    {
052                            String name = args[i];
053                            if ("--notoolkit".equals(name))
054                            {
055                                    useToolkit = false;
056                            }
057                            else
058                            {
059                                    File file = new File(name);
060                                    if (file.isFile())
061                                    {
062                                            tester.addInputFileName(name);
063                                    }
064                                    else
065                                    if (file.isDirectory())
066                                    {
067                                            tester.addDirectoryTree(name);
068                                    }
069                            }
070                    }
071                    tester.setUseToolkit(useToolkit);
072                    tester.process();
073                    int total = (tester.numFailed + tester.numSuccess);
074                    System.out.println("OK:     " + tester.numSuccess + " (" + tester.numSuccess * 100.0 / total + " %)");
075                    System.out.println("Failed: " + tester.numFailed + " (" + tester.numFailed * 100.0 / total + " %)");
076                    System.out.println("Total:  " + total + " (100.0 %)");
077            }
078    
079            /**
080             * Tries to load an image from a file.
081             * Prints a message to standard output and increases certain internal counters
082             * for statistics.
083             * @param inputDirectory directory where the file resides
084             * @param inputFileName name of file
085             * @param outputDirectory not used, this argument is demanded by the parent class+
086             */
087            public void processFile(String inputDirectory, String inputFileName, String outputDirectory)
088            {
089                    File file = new File(inputDirectory, inputFileName);
090                    String name = file.getAbsolutePath();
091                    System.out.print(name);
092                    PixelImage image;
093                    try
094                    {
095                            if (useToolkit)
096                            {
097                                    image = ToolkitLoader.loadViaToolkitOrCodecs(name, true, null);
098                            }
099                            else
100                            {
101                                    image = ImageLoader.load(name);
102                            }
103                    }
104                    catch (Exception e)
105                    {
106                            image = null;
107                    }
108                    if (image == null)
109                    {
110                            numFailed++;
111                            System.out.println(" Failed.");
112                    }
113                    else
114                    {
115                            numSuccess++;
116                            System.out.println(" OK. Width=" + image.getWidth() + ", height=" + image.getHeight() + " pixels.");
117                    }
118            }
119    
120            /**
121             * Specifies whether java.awt.Toolkit is supposed to be used when 
122             * trying to load an image.
123             * @param newValue boolean, true if Toolkit is to be used, false otherwise
124             */
125            public void setUseToolkit(boolean newValue)
126            {
127                    useToolkit = newValue;
128            }
129    }