File: ImageLoadTester.java

package info (click to toggle)
java-imaging-utilities 0.14.2%2B3-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,304 kB
  • ctags: 3,737
  • sloc: java: 31,190; sh: 238; xml: 30; makefile: 19
file content (129 lines) | stat: -rw-r--r-- 3,842 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * ImageLoadTester
 * 
 * Copyright (c) 2003 Marco Schmidt.
 * All rights reserved.
 */

package net.sourceforge.jiu.apps;

import java.io.File;
import net.sourceforge.jiu.codecs.ImageLoader;
import net.sourceforge.jiu.data.PixelImage;
import net.sourceforge.jiu.gui.awt.ToolkitLoader;
import net.sourceforge.jiu.ops.BatchProcessorOperation;

/**
 * Command line program that tries to load images from files,
 * thus testing the built-in and / or Toolkit's codecs.
 * Start the program with file names or directory names as arguments.
 * Directory names will lead to the inclusion of that complete directory tree
 * in the list of files to test.
 * Add the argument <code>--notoolkit</code> in order to keep this 
 * program from trying to load images via java.awt.Toolkit.
 * The program will process all files and print one line per file
 * that tells the result of the loading process.
 * At the end, three lines with statistics (failed / successful / total
 * loading attempts) are printed.
 * Note that you may want to give the JVM more memory if large images
 * are stored in those files you want to test.
 * Example: 
 * <pre>java -mx300m net.sourceforge.jiu.apps.ImageLoadTester *.jpg</pre>
 * This gives 300 MB to the JVM.
 * @author Marco Schmidt
 * @since 0.11.0
 */
public class ImageLoadTester extends BatchProcessorOperation
{
	private boolean useToolkit;
	private int numFailed;
	private int numSuccess;

	/**
	 * Main method of this command line program.
	 * @param args program arguments, provided by the Java Virtual Machine, must be file or directory names
	 */
	public static void main(String[] args) throws Exception
	{
		ImageLoadTester tester = new ImageLoadTester();
		boolean useToolkit = true;
		for (int i = 0; i < args.length; i++)
		{
			String name = args[i];
			if ("--notoolkit".equals(name))
			{
				useToolkit = false;
			}
			else
			{
				File file = new File(name);
				if (file.isFile())
				{
					tester.addInputFileName(name);
				}
				else
				if (file.isDirectory())
				{
					tester.addDirectoryTree(name);
				}
			}
		}
		tester.setUseToolkit(useToolkit);
		tester.process();
		int total = (tester.numFailed + tester.numSuccess);
		System.out.println("OK:     " + tester.numSuccess + " (" + tester.numSuccess * 100.0 / total + " %)");
		System.out.println("Failed: " + tester.numFailed + " (" + tester.numFailed * 100.0 / total + " %)");
		System.out.println("Total:  " + total + " (100.0 %)");
	}

	/**
	 * Tries to load an image from a file.
	 * Prints a message to standard output and increases certain internal counters
	 * for statistics.
	 * @param inputDirectory directory where the file resides
	 * @param inputFileName name of file
	 * @param outputDirectory not used, this argument is demanded by the parent class+
	 */
	public void processFile(String inputDirectory, String inputFileName, String outputDirectory)
	{
		File file = new File(inputDirectory, inputFileName);
		String name = file.getAbsolutePath();
		System.out.print(name);
		PixelImage image;
		try
		{
			if (useToolkit)
			{
				image = ToolkitLoader.loadViaToolkitOrCodecs(name, true, null);
			}
			else
			{
				image = ImageLoader.load(name);
			}
		}
		catch (Exception e)
		{
			image = null;
		}
		if (image == null)
		{
			numFailed++;
			System.out.println(" Failed.");
		}
		else
		{
			numSuccess++;
			System.out.println(" OK. Width=" + image.getWidth() + ", height=" + image.getHeight() + " pixels.");
		}
	}

	/**
	 * Specifies whether java.awt.Toolkit is supposed to be used when 
	 * trying to load an image.
	 * @param newValue boolean, true if Toolkit is to be used, false otherwise
	 */
	public void setUseToolkit(boolean newValue)
	{
		useToolkit = newValue;
	}
}